package com.wpre.vampiremq.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wpre.vampiremq.common.ConsumerEnv;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description: MessageQueue,存储消息的队列
 * User: LianBao
 * Date: 2025-01-11
 * Time: 15:46
 */

public class MSGQueue {

    //队列的身份标识
    private String name;

    //队列是否需要持久化存储,true表示持久化保存
    private boolean durable = false;

    //true-->这个队列只能被一个消费者使用,别人不能使用,false-->大家都可以使用,暂时不实现
    private boolean exclusive = false;

    //自动删除,true-->队列没人使用后自动删除,false-->队列没人使用后不自动删除,暂时不实现
    private boolean autoDelete = false;

    //额外参数选项,自己实现
    private Map<String, Object> arguments = new HashMap<>();

    //当前队列有哪些消费者订阅了
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();

    //记录当前取到了第几关消费者,方便轮询(考虑线程安全)
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    //添加新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv) {

        consumerEnvList.add(consumerEnv);

    }

    //挑选一个订阅者来处理当前的消息(轮询的方法)
    public ConsumerEnv chooseConsumer() {
        if (consumerEnvList.size() == 0) {
            //当前没有人订阅
            return null;
        }
        //计算当前要获取的元素下标
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();//consumerSeq++

        return consumerEnvList.get(index);
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public void setArguments(String arguments) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(arguments, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key, value);
    }


}
